﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SYH.Utils
{
    public class ImageUploadHelper
    {
        /// <summary>
        ///生成缩略图
        /// </summary>
        /// <param name="originalImageName">图片名称</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <param name="model">缩略模式</param>
        public static void MakeThumNail(string originalImageName, int width, int height)
        {
            string thumNailPath = string.Empty;
            string temp = width + "-" + height;
            if (originalImageName.Contains("original"))
            {
                thumNailPath = originalImageName.Replace("original", temp);
            }
            MakeThumNail(originalImageName, thumNailPath, width, height, "qhw");

        }

        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="orginalImagePat">原图片地址</param>
        /// <param name="thumNailPath">缩略图地址</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="model">生成缩略的模式,</param>
        public static void MakeThumNail(string originalImagePath, string thumNailPath, int width, int height, string model)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);


            int thumWidth = width;      //缩略图的宽度
            int thumHeight = height;    //缩略图的高度

            int x = 0;
            int y = 0;

            int originalWidth = originalImage.Width;    //原始图片的宽度
            int originalHeight = originalImage.Height;  //原始图片的高度

            switch (model.ToLower())
            {
                case "hw":      //指定高宽缩放,可能变形
                    break;
                case "w":       //指定宽度,高度按照比例缩放
                    thumHeight = originalImage.Height * width / originalImage.Width;
                    break;
                case "h":       //指定高度,宽度按照等比例缩放
                    thumWidth = originalImage.Width * height / originalImage.Height;
                    break;
                case "cut":
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)thumWidth / (double)thumHeight)
                    {
                        originalHeight = originalImage.Height;
                        originalWidth = originalImage.Height * thumWidth / thumHeight;
                        y = 0;
                        x = (originalImage.Width - originalWidth) / 2;
                    }
                    else
                    {
                        originalWidth = originalImage.Width;
                        originalHeight = originalWidth * height / thumWidth;
                        x = 0;
                        y = (originalImage.Height - originalHeight) / 2;
                    }
                    break;
                case "qhw": //企汇网金铺缩略图，缩略图比例与原图一致；原图宽大于高时,缩略图实际的宽等于缩略图标准宽;否则缩略图实际的高等于缩略图标准高
                    if (originalWidth > originalHeight)
                    {
                        if (originalWidth > width) //如果原图的宽小于标准宽不压缩
                        {
                            thumWidth = width;
                            thumHeight = (int)(originalHeight * width / originalWidth);
                        }
                    }
                    else
                    {
                        if (originalHeight > height)
                        {
                            thumHeight = height;
                            thumWidth = thumHeight * originalWidth / originalHeight;
                        }
                    }
                    if (originalWidth <= width && originalHeight <= height)
                    {
                        thumWidth = originalWidth;
                        thumHeight = originalHeight;
                    }

                    break;
                default:
                    break;
            }

            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(thumWidth, thumHeight);

            //新建一个画板
            System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量查值法
            graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量，低速度呈现平滑程度
            graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            graphic.Clear(System.Drawing.Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            graphic.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, thumWidth, thumHeight), new System.Drawing.Rectangle(x, y, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel);

            try
            {
                string dir = Path.GetDirectoryName(thumNailPath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                graphic.Dispose();
            }

        }
    }
}
